package com.z.admin.controller;

import com.z.admin.entity.UploadFiles;
import com.z.base.vo.Result;
import com.z.utils.KitFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/files")
@Slf4j
public class AdminFilesController {

    @PostMapping(value = "/uploadImg")
    @ResponseBody
    public Object uploadImg(@RequestParam("layuiFile") MultipartFile[] layuiFile, HttpServletRequest request, HttpServletResponse response){
        log.info("进入layui图片上传接口》》》》》》》》》》》》》》》");
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        KitFileUtil kitFileUtil=new KitFileUtil();
        map = kitFileUtil.upload(layuiFile, request, response, "");
        String error = map.get("error").toString();
        if("101".equals(error)){
            return Result.error((String)map.get("message"));
        }
        map2.put("error",0);
        String url[]=(String[])map.get("url");
        map2.put("url",url[0]);
        UploadFiles file = new UploadFiles();
        file.setName((String)map.get("fileName"));
        file.setUrl(url[0]);
        file.setCreateTime(new Date());
        file.setType("tp");
        return Result.success(map2);
    }
}
